﻿var examTest = {
	init : function() {
		
		/*function addPrefix(n) {
			  if (n < 10) {
			      n = '0' + n;
			  }
			  return n;
			}
			var times = 2 * 60 * 60; //一个小时，按秒计算，自己调整!   
			function CountDown() {
			    if (times >= 0) {
			        minutes = Math.floor(times / 60);
			        seconds = Math.floor(times % 60);
			        msg =  addPrefix(minutes) + ":" + addPrefix(seconds);
			        $('#timeEnd').text(msg);
			        alert($('#timeEnd'))
			        if (times == 5 * 60) alert('注意，还有5分钟!');
			        --times;
			    } else {
			        clearInterval(timer);
			        alert("时间到，结束!");
			    }
			}
			timer = setInterval("CountDown()", 1000);*/

		
		    var url = window.location.href;
		    //将"进行URLencode编码
		    var str = url.indexOf(encodeURI('"'));
		    //从"截取,得到字符串再转成数字
		    strId = decodeURI(url.substring(str)).replace(/[^0-9]/ig,"") ;
		    
			$.post(window.ctx+'/ExammingServlet?command=showPaper',  {id : strId}, function(data) {
				if (data) {
					
					var jumbotron = $('.jumbotron');
					jumbotron.hide();
					contain = $('#contain');
					var jsonObj=$.parseJSON(data[0]);
					
					times = jsonObj.totalMinutes * 60;
					
					console.info(data[0])
					var singleList=jsonObj.singleList;
					var multipleList=jsonObj.multipleList;
					var judgeList=jsonObj.judgeList;	
					var CSS =[];
					if(singleList){
						for(var i = 0;i < singleList.length;i++){
							CSS.push('<p class="text-left">'+
							'<h4 id="singleList'+(i+1)+'">　　　第'+(i+1)+'题：'+singleList[i].question +'（ ）'+'</h4>'+
							 '</p>'+
							 '<label>'+
							 '　　　　　　<input type="radio" name="singleName'+(i+1)+'" value="A"/><b>'+'A. '+singleList[i].answerB +'</b></p>'+
							 '</label><br/>'+
							 '<label>'+
							 '　　　　　　<input type="radio" name="singleName'+(i+1)+'" value="B"/><b>'+'B. '+singleList[i].answerB +'</b></p>'+
							 '</label><br/>'+
							 '<label>'+
							 '　　　　　　<input type="radio" name="singleName'+(i+1)+'" value="C"/><b>'+'C. '+singleList[i].answerC +'</b></p>'+
							 '</label><br/>'+
							 '<label>'+
							 '　　　　　　<input type="radio" name="singleName'+(i+1)+'" value="D"/><b>'+'D. '+singleList[i].answerD +'</b>'+
							'</label>');
						}
					}
					var CSSM =[];
					if(multipleList){
						for(var i = 0;i < multipleList.length;i++){
							CSSM.push('<h4 id="multipleList'+(i+1)+'">　　　第'+(i+1)+'题：'+multipleList[i].question +'（ ）</h4>'+
							'<label>'+
							'　　　　　　<input type="hidden" name="id" value="'+multipleList[i].id+'"/>'+
							'<input type="checkbox" name="multipleName'+(i+1)+'" value="1"/><b>'+'A.'+multipleList[i].answerA +'</b>'+
							'</label></p>'+
							' <label>'+
							'　　　　　　<input type="checkbox" name="multipleName'+(i+1)+'" value="2"/><b>'+'B.'+multipleList[i].answerB +'</b>'+
							'</label></p>'+
							' <label>'+
							'　　　　　　<input type="checkbox" name="multipleName'+(i+1)+'" value="3"/><b>'+'C.'+multipleList[i].answerC +'</b>'+
							'</label></p>'+
							' <label>'+
							'　　　　　　<input type="checkbox" name="multipleName'+(i+1)+'" value="4"/><b>'+'D.'+multipleList[i].answerD +'</b>'+
							'</label></p>');	
						}
					}
					
					var CSSJ =[];
					if(judgeList){
						for(var i = 0;i < judgeList.length;i++){
							CSSJ.push('<h4 id="judgeList'+(i+1)+'">　　　第'+(i+1)+'题：'+judgeList[i].question +'（ ）</h4>'+
							' <label>'+
							'　　　　　　<input type="radio" name="judgeName'+(i+1)+'" value="1"/><b>'+'正确. '+judgeList[i].answer +'</b></p>'+
							'</label><br/>'+
							' <label>'+
							'　　　　　　<input type="radio" name="judgeName'+(i+1)+'" value="2"/><b>'+'错误. '+judgeList[i].answer +'</b></p>'+
							' </label></p>');
						}
					}
					contain.append('<h1><p class="text-center">'+jsonObj.name+'期末考试</p></h1>'+
							'<h4> <p class="text-center">(时间：'+jsonObj.totalMinutes +'分钟,分数:'+jsonObj.totalPoint+'分)</p></h4>'+
						'<div class="container" style="border:1px solid rgb(25, 170, 141);border-radius:10px">'+
							'<form id="examAnswerForm" action = "" method="post">'+
								'<div class="panel panel-info " style="position:fixed;margin-left:900px">'+
								  '<div class="panel-heading">'+
									  '<p  class=" text-right" >'+
										'<button class="btn btn-primary" type="button">'+
											'倒计时： <span class="badge" id="timeEnd" style="font-size:18px">'+jsonObj.totalMinutes+':00</span>'+
										'</button>'+
									  '</p >'+
									  '<p><h3  class=" text-center">答题卡</h3>'+
									  '</p >'+
								  '</div>'+
								  '<table class="table table-bordered text-center">'+
								  	'<tr>'+
								  		'<td class="active">1</td>'+
								  		'<td class="success">2</td>'+
								  		'<td class="warning">3</td>'+
								  		'<td class="danger">4</td>'+
								  		'<td class="info">5</td>'+
								  	'</tr>'+
								  	'<tr>'+
								  		'<td class="active">6</td>'+
								  		'<td class="success">7</td>'+
								  		'<td class="warning">8</td>'+
								  		'<td class="danger">9</td>'+
								  		'<td class="info">10</td>'+
									'</tr>'+
									  	'<tr>'+
								  		'<td class="active">11</td>'+
								  		'<td class="success">12</td>'+
								  		'<td class="warning">13</td>'+
								  		'<td class="danger">14</td>'+
								  		'<td class="info">15</td>'+
								  	'</tr>'+
								  '</table>'+
								'</div>'+
								'<p ><h3>一、单选题  (共'+jsonObj.singleOptionNumber+'题 ｘ '+jsonObj.singleOptionEachPoint+'分)</h3></p>'+
									CSS+
								'<p ><h3>二、多选题(共'+jsonObj.multiOptionNumber+'题 ｘ '+jsonObj.multiOptionEachPoint+'分)</h3></p>'+
									CSSM+
								'<p ><h3>三、判断题(共'+jsonObj.judgeNumber+'题 ｘ '+jsonObj.judgeEachPoint+'分)</h3></p>'+
									CSSJ+
								'<p class="text-center"><button type="submit" id = "sub" class="btn btn-success ">提交试卷</button>'+
							'</form>'+
						'</div>'
					);
				}
				$(window).unload( function (){ 
					console.info("Bye now!"); 
					$.ajax({
						url: window.ctx+'/ExammingServlet?command=getTime&time='+times,
				        type: "post",
				        success: function(data) {
				        	
				        },
				    });
				});
				
				function addPrefix(n) {
				  if (n < 10) {
				      n = '0' + n;
				  }
				  return n;
				}
				timer = setInterval(function () {
				    if (times >= 0) {
				        minutes = Math.floor(times / 60);
				        seconds = Math.floor(times % 60);
				        msg =  addPrefix(minutes) + ":" + addPrefix(seconds);
				        $('#timeEnd').text(msg);
				        --times;
				    }else{
				    	$('#examAnswerForm').submit();
				    }
				}, 1000);
					
				$('#examAnswerForm').submit(function(){
					$.ajax({
				        url: window.ctx+'/ExammingServlet?command=sub',
				        type: "post",
				        data: $('#examAnswerForm').serialize(),
				        success: function(data) {
				            
				        },
				    });
				});
				console.info(times)
			}, 'json');
		},
	};
	
	
	
	$(function() {
		examTest.init();
	});
